package com.dingcan.biao3;

import java.sql.ResultSet;
import java.sql.SQLException;

public class NewsDaoImp1 extends BaseDao implements NewsDao {

    //查看
    @Override
    public void getNewsByTitle() {
        try{
            String sql = "SELECT * FROM candai";
            Object[] params ={};
            ResultSet re = this.executeSQL(sql ,params);
            while (re.next()){
                int id=re.getInt("id");
                String userName = re.getString("name");
                int num = re.getInt("num");
                String time = re.getString("time");
                String address = re.getString("address");
                String price = re.getString("price");
                String state = re.getString("state");
                System.out.println(id +"\t"+userName +"\t"+num + "\t"+time +"\t"+address +"\t"+price + "\t" + state);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
   //增加
    @Override
    public void addNews( String name,int num,String time,String address,String price,String state) {
        try {
            String sql="INSERT INTO candai(name,num,time,address,price,state)VALUES(?,?,?,?,?,?) ";
            Object[] params ={name,num,time,address,price,state};
            int i = this.executeUpdate(sql,params);
            if (i>0){
                System.out.println("新增成功");
            }
        } finally {
            try{
                preparedStatement.close();
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }

        }
    }

    @Override
    public void deleteNews(int caidan_id) {
       try{
           this.getConnection();
           String sql = "DELETE FROM candai WHERE id=?";
           preparedStatement = connection.prepareStatement(sql);
           preparedStatement.setInt(1,caidan_id);
           int i=preparedStatement.executeUpdate();
           if (i>0){
               System.out.println("删除成功");
           }
       }catch (SQLException e){
           e.printStackTrace();
       }

    }

    @Override
    public  void updateNews(int id, String state) throws SQLException {
        try{
            String sql="UPDATE candai SET state=?WHERE id=?";
            Object[] params ={state,id};
            int i=this.executeUpdate(sql,params);
            if (i>0){
                System.out.println("修改成功");
            }
        } finally {
            preparedStatement.close();
            connection.close();
        }


    }

}
